/*
* Smoke.java
* Created on  202015/6/29 9:25
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/29       longshihui    初始版本
*
*/

package com.infitecs.eyas.enthalpy.service;

import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.enthalpy.dao.SmokeDao;
import com.infitecs.eyas.enthalpy.model.Smoke;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.util.DoubleUtil;
import com.infitecs.eyas.util.Parameter;
import com.infitecs.eyas.util.ParametersUtil;
import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Service
public class SmokeService {
    @Autowired
    private SmokeDao smokeDao;
    @Autowired
    private ExpressionDao expressionDao;

    public void updateCx(Param param){
        smokeDao.updateCx(param);
    }

    public Smoke getSmoke(int caseId){
        return smokeDao.getSmoke(caseId);
    }

    public void addSmoke(int caseId){
        smokeDao.addSmoke(caseId);
    }

    public Double calC10(Param param) {
        Formula formula = expressionDao.getFormulaByName(Constant.ENTHALPY.SMOKE_C10.toString());
        Smoke smoke = smokeDao.getSmoke(param.getCaseId());
        Double result = null;
        try {
            Expression expression = ExpressionFactory.createExpression(formula.getParamer() + formula.getFormulaDescription());
            Set<Parameter> params = new HashSet<>();
            params.add(new Parameter("C1",smoke.getC1()));
            params.add(new Parameter("C2",smoke.getC2()));
            params.add(new Parameter("C3",smoke.getC3()));
            params.add(new Parameter("C4",smoke.getC4()));
            params.add(new Parameter("C5",smoke.getC5()));
            params.add(new Parameter("C6",smoke.getC6()));
            params.add(new Parameter("C7",smoke.getC7()));
            params.add(new Parameter("C8",smoke.getC8()));
            params.add(new Parameter("C9",smoke.getC9()));
            result = DoubleUtil.getInstance().get3Double(expression.evaluate(ParametersUtil.getParameters(params)).getRealValue());
            param.setParamValue(result);
            updateCx(param);
        } catch (ParsingException e) {
            e.printStackTrace();
        } catch (EvalException e) {
            e.printStackTrace();
        }
        return result;
    }
}
